﻿#include "Frm03.h"
//#include "curl.h"
//#include <sha.h>
//#include <evp.h>
//#include <ctime>
//#include <random>
//#include <iostream>
//#include <bio.h>
//#include <buffer.h>
#include <util/StringUtil.h>

Frm03::Frm03(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm03::~Frm03() {
}

void Frm03::onInit(){
}

void Frm03::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(1, 52, 198, 48, false);
	wMButton1->setPressedArg("getDeviceInfo", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("getDeviceInfo", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm03::wMButton1_clk_cb);

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(200, 52, 198, 48, false);
	wMButton2->setPressedArg("getDeviceInfo", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("getDeviceInfo", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm03::wMButton2_clk_cb);

	/*----------------------------- Button : wMButton3                   -----------------------------*/
	wMButton3 = new STTButton(p_rt, this, "wMButton3", 0);
	wMButton3->setMainArg(399, 52, 198, 48, false);
	wMButton3->setPressedArg("SystemReboot", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->setReleasedArg("SystemReboot", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->onEventHandler(Event::CLICKED, this, (EHandler) &Frm03::wMButton3_clk_cb);

	/*----------------------------- Button : wMButton4                   -----------------------------*/
	wMButton4 = new STTButton(p_rt, this, "wMButton4", 0);
	wMButton4->setMainArg(1, 1, 98, 48, false);
	wMButton4->setPressedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton4->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton4->setReleasedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton4->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton4->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*called after widget created.*/

	return; //createParts END
}

void Frm03::onLoad(){
}

bool Frm03::onClosing(){
	return true;
}

void Frm03::onDispose(){
}





void Frm03::wMButton1_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（功能键）的点击事件*/
//从ONVIF_Device_TestTool中查看你需要的请求内容，这里是GetDeviceInformation
//	std::string postField = R"(<?xml version="1.0" encoding="utf-8"?>
//<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:tt="http://www.onvif.org/ver10/schema">
//  <s:Header xmlns:s="http://www.w3.org/2003/05/soap-envelope">
//    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
//      <wsse:UsernameToken>
//        <wsse:Username>test</wsse:Username>
//        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">0W0CitWvEPy7TNWDQf3HNkJd3go=</wsse:Password>
//        <wsse:Nonce>5+RZ+qERszeqUhhZXDvcjQ==</wsse:Nonce>
//        <wsu:Created>2025-05-15T14:27:10Z</wsu:Created>
//      </wsse:UsernameToken>
//    </wsse:Security>
//  </s:Header>
//  <soap:Body>
//    <tds:GetDeviceInformation />
//  </soap:Body>
//</soap:Envelope>)";

	std::string postField = R"(<?xml version="1.0" encoding="utf-8"?>
				<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:tt="http://www.onvif.org/ver10/schema">
				  <s:Header xmlns:s="http://www.w3.org/2003/05/soap-envelope">
	<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
	  <wsse:UsernameToken>
	    <wsse:Username>test</wsse:Username>
	    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">MWGfEVHt1KbaBjlOR5x80ND5xj8=
	</wsse:Password>
	    <wsse:Nonce>XPbueSzfBeG6K2MlxBpfEA==
	</wsse:Nonce>
	    <wsu:Created>2025-05-15T14:30:01Z</wsu:Created>
	  </wsse:UsernameToken>
	</wsse:Security>
	</s:Header>
				  <soap:Body>
				    <tds:GetDeviceInformation />
				  </soap:Body>
				</soap:Envelope>)";
    // ONVIF 设备服务地址
    const string url = "http://192.168.1.64/onvif/device_service";
    HmiApp::request(url, postField);
}




void Frm03::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（功能键）的点击事件*/
	// 构建 SOAP 请求体[1](@ref)
	const string url = "http://192.168.1.64/onvif/device_service";
	string username = "test";
	string password = "whxtt2022";
	const std::string soap_body =
			R"(<?xml version="1.0" encoding="utf-8"?>
			<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:tt="http://www.onvif.org/ver10/schema">
			  <s:Header xmlns:s="http://www.w3.org/2003/05/soap-envelope">)"
			    + HmiApp::generate_wsse_header(username, password) +
			    R"(</s:Header>
			  <soap:Body>
			    <tds:GetDeviceInformation />
			  </soap:Body>
			</soap:Envelope>)";
	HmiApp::request(url, soap_body);
}

void Frm03::wMButton3_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（功能键）的点击事件*/
	// 构建 SOAP 请求体[1](@ref)
	const string url = "http://192.168.1.64/onvif/device_service";
	string username = "test";
	string password = "whxtt2022";
	const std::string soap_body = R"(<?xml version="1.0" encoding="utf-8"?>
			<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:tt="http://www.onvif.org/ver10/schema">
			  <s:Header xmlns:s="http://www.w3.org/2003/05/soap-envelope">)" + HmiApp::generate_wsse_header(username, password) + R"(</s:Header>
			  <soap:Body>
			    <tds:SystemReboot />
			  </soap:Body>
			</soap:Envelope>)";
	HmiApp::request(url, soap_body);
}
